<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户信息表编辑--${site.name}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="description" content="${site.description}"/>
    <meta name="keywords" content="${site.keywords}"/>
    <meta name="author" content="${site.author}"/>
    <link rel="icon" href="${site.logo}">
    <link rel="stylesheet" href="${base}/static/layui/css/layui.css" media="all"/>
    <style type="text/css">
        .layui-form-item .layui-inline {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }

        .layui-form-item .role-box {
            position: relative;
        }

        .layui-form-item .role-box .jq-role-inline {
            height: 100%;
            overflow: auto;
        }

    </style>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%">
    <input value="${electricityUser.id}" name="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityUser.userName}" name="userName"
                   lay-verify="required" placeholder="请输入昵称">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityUser.phone}" name="phone" id="phone"
                   lay-verify="required" placeholder="请输入手机号">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">销售编码</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityUser.saleManCode}" name="saleManCode"
                   placeholder="请输入销售编码" readonly>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">市场成员类型</label>
        <div class="layui-input-block">
            <select name="memberType" id="memberType" disabled="true">
                <option value="" selected="">请选择市场成员类型</option>
                <@my type="electricity_company_member_type">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.memberType == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.companyName}" name="companyName"
                   placeholder="请输入公司名称">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">社会统一信用代码</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.creditCode}" name="creditCode"
                   placeholder="请输入社会统一信用代码" readonly>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">企业性质</label>
        <div class="layui-input-block">
            <select name="businessProperty">
                <option value="" selected="">请选择企业性质</option>
                <@my type="electricity_company_business_property">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.businessProperty == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>

        </div>
    </div>

    <div class="layui-form-item fd" style="display: none;">
        <label class="layui-form-label">发电类型</label>
        <div class="layui-input-block">
            <select name="powerGenerationType">
                <option value="" selected="">请选择发电类型</option>
                <@my type="electricity_company_Power_generation_type">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.powerGenerationType == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>

        </div>
    </div>
    <div class="layui-form-item fd" style="display: none;">
        <label class="layui-form-label">装机容量</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.installedCapacity}"
                   name="installedCapacity" placeholder="请输入装机容量">

        </div>
    </div>
    <div class="layui-form-item fd" style="display: none;">
        <label class="layui-form-label">上年度上网电量</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.internetPowerLastYear}"
                   name="internetPowerLastYear" placeholder="请输入上年度上网电量">

        </div>
    </div>
    <div class="layui-form-item sd" style="display: none;">
        <label class="layui-form-label">注册资本金</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.registerCapital}" name="registerCapital"
                   placeholder="请输入注册资本金">

        </div>
    </div>
    <div class="layui-form-item sd" style="display: none;">
        <label class="layui-form-label">是否拥有配电网</label>
        <div class="layui-input-block">
            <select name="isHaveNetwork">
                <option value="" selected="">请选择是否拥有配电网</option>
                <@my type="electricity_company_is_have_network">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.isHaveNetwork == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>

        </div>
    </div>
    <div class="layui-form-item dl" style="display: none;">
        <label class="layui-form-label">用户类型</label>
        <div class="layui-input-block">
            <select name="userType">
                <option value="" selected="">请选择用户类型</option>
                <@my type="electricity_company_user_type">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.userType == r.value)> selected="" </#if>
                                readonly>${r.label}</option>
                    </#list>
                </@my>
            </select>
        </div>
    </div>
    <div class="layui-form-item dl" style="display: none;">
        <label class="layui-form-label">用户电压等级</label>
        <div class="layui-input-block">
            <select name="userVoltageGrade" disabled="true">
                <option value="" selected="">请选择用户电压等级</option>
                <@my type="electricity_company_user_voltage_grade">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.userVoltageGrade == r.value)> selected="" </#if>
                                readonly>${r.label}</option>
                    </#list>
                </@my>
            </select>
        </div>
    </div>
    <div class="layui-form-item dl" style="display: none;">
        <label class="layui-form-label">是否园区用户</label>
        <div class="layui-input-block">
            <select name="isCampusUser">
                <option value="" selected="">请选择是否园区用户</option>
                <@my type="electricity_company_is_campus_user">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityCompany.isCampusUser == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>

        </div>
    </div>
    <div class="layui-form-item dl" style="display: none;">
        <label class="layui-form-label">上年用电量</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.energyUsedLastYear}"
                   name="energyUsedLastYear" placeholder="请输入上年用电量" readonly>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">信用等级</label>
        <div class="layui-input-block">
            <@my type="electricity_company_credit_grade" >
                <#list result as r>
                    <input type="radio" name="creditGrade" value="${r.value}"
                           title="${r.label}" <#if (electricityCompany.creditGrade == r.value)> checked="" </#if> >
                </#list>
            </@my>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">营业执照</label>
        <div class="layui-input-block">
            <input type="hidden" class="layui-input" name="businessLicense" id="businessLicense"
                   value="${electricityCompany.businessLicense}">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test_businessLicense">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo_businessLicense"
                         <#if (electricityCompany.businessLicense??)>src="${electricityCompany.businessLicense}"</#if> >
                    <p id="demoText_businessLicense"></p>
                </div>
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">企业地址：</label>
        <input value="${businessProvince}" type="hidden" id="echoPr">
        <div class="layui-input-inline company-address">
            <select id="province" lay-filter="province" class="pesSelect" name="businessProvince">
                <option value="请选择省">请选择省</option>
            </select>
        </div>
        <div class="layui-input-inline company-address">
            <input value="${businessCity}" type="hidden" id="echoCity">
            <select id="city" lay-filter="city" class="pesSelect" name="businessCity">
                <option value="请选择市">请选择市</option>
            </select>
        </div>
        <div class="layui-input-inline company-address">
            <input value="${businessCountry}" type="hidden" id="echoCoun">
            <select id="district" lay-filter="district" class="pesSelect " name="businessCountry">
                <option value="请选择地区">请选择地区</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址详细信息</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.businessDetail}" name="businessDetail"
                   placeholder="请输入地址详细信息">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经办人</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.managerName}" name="managerName"
                   placeholder="请输入经办人">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经办人电话</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityCompany.managerPhone}" name="managerPhone"
                   placeholder="请输入经办人电话">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input type="hidden" class="layui-input" name="img" id="img" value="${electricityUser.img}">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test_img">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo_img"
                         <#if (electricityUser.img??)>src="${electricityUser.img}"</#if> >
                    <p id="demoText_img"></p>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">入市有效期</label>
        <div class="layui-input-block">
            <input type="text" name="dueDate" id="dueDate"
                   <#if (electricityCompany.dueDate)??>value="${electricityCompany.dueDate?string('yyyy-MM-dd')}"</#if>
                   placeholder="请选择到期日期" autocomplete="off" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addElectricityUser">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="${base}/static/layui/layui.js"></script>
<script type="text/javascript" src="${base}/static/js/tools.js"></script>
<script type="text/javascript" src="${base}/static/layui/lay/modules/laydate.js"></script>
<script type="text/javascript" src="${base}/static/js/libs/jquery-2.1.1.min.js"></script>
<script>
    function saveData(KEY, data) {
        window.localStorage.setItem(KEY, JSON.stringify(data))

    }

    function readData(KEY) {
        return JSON.parse(window.localStorage.getItem(KEY) || '[]')

    }

    function removeData(KEY) {
        if (window.localStorage) {
            localStorage.removeItem(KEY)
        }
    }

    var areaData = []
    var provinceArray = new Array();  // 省
    var cityArray = new Array(); // 市
    var cityList = [];

    function renderForm() {
        layui.use('form', function () {
            var form = layui.form
            form.render();
        });
    }

    layui.use(['form', 'jquery', 'layer', 'laydate', 'upload'], function () {
        var form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            upload = layui.upload,
            layer = layui.layer;
        var localData = readData('AREA-DATA')
        if (localData && localData.length !== 0) {
            areaData = localData
            renderProvince()   // 省市联动
        } else {
            $.ajax({
                url: "${base}/admin/electricityUser/getAllArea",
                async: false,
                type: 'post',
                success: function (data) {
                    if (data && data.length !== 0) {
                        areaData = data
                        saveData('AREA-DATA', data)
                        renderProvince()   // 省市联动
                    }
                }
            })
        }

        function renderProvince() {
            var provinceTag = document.getElementById("province");//设置默认值
            for (var i = 0; i < areaData.length; i++) {
                var province = areaData[i];
                var provinceName = province.addressName;
                provinceArray[i] = provinceName;
                provinceTag.add(new Option(provinceName, province.id));
                renderForm()
            }
        }

        /*获取当前城市*/
        function getCity(index) {
            var cityTag = document.getElementById("city");
            if (index != 0) {
                var provinceName = provinceArray[index - 1];
                for (var n = 0; n < areaData.length; n++) {
                    var province = areaData[n];
                    if (province.addressName == provinceName) {
                        cityList = province.child;
                        cityTag.innerHTML = "";
                        for (var c = 0; c < cityList.length; c++) {
                            var city = cityList[c];
                            var addressName = city.addressName;
                            cityArray[c] = addressName;
                            cityTag.add(new Option(addressName, city.id));
                        }
                    }
                }
            } else {
                cityTag.innerHTML = "";
                cityTag.add(new Option("请选择市"));
            }
        }

        /*获取当前地区*/
        function getDistrict(proIndex, cityIndex) {
            var areaTag = document.getElementById("district");
            if (proIndex !== 0) {
                var cityName = cityArray[cityIndex];
                for (var m = 0; m < cityList.length; m++) {
                    var city1 = cityList[m];
                    if (city1.addressName == cityName) {
                        var areaList = city1.child;
                        areaTag.innerHTML = "";
                        for (var a = 0; a < areaList.length; a++) {
                            var area = areaList[a];
                            areaTag.add(new Option(area.addressName, area.id));
                        }
                    }
                }
            } else {
                areaTag.innerHTML = "";
                areaTag.add(new Option("请选择地区"));
            }
        }

        var echoPr = $('#echoPr').val()
        var echoCity = $('#echoCity').val()
        var echoCoun = $('#echoCoun').val()
        if (echoPr != "" || echoCity != "" || echoCoun != "") {
            $("#province").find("option[value=" + echoPr + "]").prop("selected", true);
            var proIndex = $('#province').get(0).selectedIndex
            getCity(proIndex)
            $("#city").find("option[value=" + echoCity + "]").prop("selected", true);
            var cityIndex = $('#city').get(0).selectedIndex
            getDistrict(proIndex, cityIndex)
            $("#district").find("option[value=" + echoCoun + "]").prop("selected", true);
            renderForm()
        }
        /*选择省*/
        form.on('select(province)', function (data) {
            var proIndex = $('#province').get(0).selectedIndex
            var cityIndex = $('#city').get(0).selectedIndex
            getCity(proIndex)
            getDistrict(proIndex, cityIndex)
            form.render('select');
        })
        /*选择市*/
        form.on('select(city)', function (data) {
            var proIndex = $('#province').get(0).selectedIndex
            var cityIndex = $('#city').get(0).selectedIndex
            getDistrict(proIndex, cityIndex)
            form.render('select');
        })
        /*选择区*/
        form.on('select(district)', function (data) {
            form.render('select');
        })
        //获取消息模板
        /*        $.ajax({
                    type: 'post',
                    url:"
        ${base}/admin/electricityUser/getAllArea",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (data) {
                console.log(data);
                layui.form.render('select')
            }
        });*/

        //初始赋值

        laydate.render({
            elem: '#dueDate'
            <#if (electricityCompany.dueDate)??>
            , value: '${electricityCompany.dueDate?string("yyyy-MM-dd")}'
            </#if>
        });

        form.on("submit(addElectricityUser)", function (data) {
            if (null === data.field.dueDate || "" === data.field.dueDate) {
                delete data.field["dueDate"];
            } else {
                data.field.dueDate = new Date(data.field.dueDate);
            }
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            var reg = /^[1]([3-9])[0-9]{9}$/;
            var phone = $("#phone").val();

            if (reg.test(phone) || phone == "") {
            } else {
                layer.open({
                    title: '提示'
                    , content: '手机号格式有误'
                    , yes: function () {
                        layer.closeAll();
                    }
                });
                return false;
            }

            //给角色赋值
            $.post("${base}/admin/electricityUser/edit", data.field, function (res) {
                layer.close(loadIndex);
                if (res.success) {
                    parent.layer.msg("用户信息表编辑成功！", {time: 1000}, function () {
                        parent.layer.close(parent.editIndex);
                        //刷新父页面
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.message);
                }
            });
            return false;
        });

        // 0 发电 1 售电 2电力
        var memberType = $("#memberType").val()
        if (memberType == 0) {
            $(".fd").show();
            $(".sd").hide();
            $(".dl").hide();
        } else if (memberType == 1) {
            $(".fd").hide();
            $(".sd").show();
            $(".dl").hide();
        } else if (memberType == 2) {
            $(".fd").hide();
            $(".sd").hide();
            $(".dl").show();
        }


        //营业执照
        var upload_picPath = upload.render({
            elem: '#test_businessLicense',
            url: '${base}/file/uploadOss/',
            field: 'test',
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo_businessLicense').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.success == false) {
                    return layer.msg('上传失败');
                }
                $("#businessLicense").val(res.data);
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText_businessLicense');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    upload_picPath.upload();
                });
            }
        });
        //头像上传
        var upload_picPath = upload.render({
            elem: '#test_img',
            url: '${base}/file/uploadOss/',
            field: 'test',
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo_img').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.success == false) {
                    return layer.msg('上传失败');
                }
                $("#img").val(res.data);
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText_img');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    upload_picPath.upload();
                });
            }
        });

    });
</script>
</body>
</html>
